//
//  RTCViewFactory.m
//  avd_sdk_plugin
//
//  Created by 3tee on 2023/6/12.
//

#import "RTCVideoViewFactory.h"



@implementation RTCVideoViewFactory

-(void)dealloc{
    NSLog(@"RTCVideoViewFactory dealloc♻️");
}

- (instancetype)initWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger {
    self = [super init];
    if (self) {
        self.messenger = messenger;
        self.rendermArr = [NSMutableArray array];
    }
    return self;
}

//设置参数的编码方式
-(NSObject<FlutterMessageCodec> *)createArgsCodec{
    return [FlutterStandardMessageCodec sharedInstance];
}

//用来创建 ios 原生view
- (NSObject<FlutterPlatformView> *)createWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id)args {
    NSDictionary *dic = (NSDictionary *)args;
    NSString * did = dic[@"data"][@"did"];
    NSLog(@"RTCVideoView  creat did = %@",did);

    NSLog(@"RTCVideoView  creat frame = %@ viewid = %d args = %@",NSStringFromCGRect(frame),viewId,args);
    RTCVideoView *view = [[RTCVideoView alloc]initWithWithFrame:frame viewIdentifier:viewId arguments:args binaryMessenger:_messenger];
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"avd_video_view_channel" binaryMessenger:_messenger];
    [channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
      if ([@"updateRenderMode" isEqualToString:call.method]) {
        // 更新渲染模式
        int mode = [args[@"mode"] intValue];
        //[view updateRenderMode:mode];
        result(nil);
      }
    }];
    self.rtcVideoView = view;
    [self.rendermArr addObject:view];
    return view;
}
@end

